using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Data;
using System.Configuration;
using System.IO;
using System.Xml;
namespace DataProvider
{
    public static class Config
    {
        public static readonly Dictionary<String,String> Items = new Dictionary<string,string>(StringComparer.OrdinalIgnoreCase);
        public static readonly Dictionary<String, List<String>> MultiItems = new Dictionary<string, List<String>>(StringComparer.OrdinalIgnoreCase);

        public static String ConfigFile = @"\Config.xml";
        public static List<String> EntityList = new List<string>();

        static Config()
        {
            ReLoadConfig();
        }

        public static void ReLoadConfig()
        {
            MultiItems.Clear();
            encrptStr = "";

            String configFile = ConfigurationManager.AppSettings["configfile"];
            ConfigFile = configFile;
            LoadConfigurationFile(configFile);
        }

        private static void LoadConfigurationFile(String fileName)
        {
            if (!File.Exists(fileName))
            {
                return;
            }
            String xmlContent = "";
            using (StreamReader reader = new StreamReader(fileName))
            {
                xmlContent = reader.ReadToEnd();
            }

            using (XmlReader reader = new XmlTextReader(xmlContent, XmlNodeType.Element, null))
            {
                reader.MoveToContent();
                do
                {
                    if (String.Compare(reader.Name, "items", true) == 0)
                    {
                        using (XmlReader itemReader = reader.ReadSubtree())
                        {
                            do
                            {
                                if (itemReader.Name != "item" || itemReader.NodeType != XmlNodeType.Element)
                                {
                                    continue;
                                }
                                String name = itemReader.GetAttribute("name");
                                String value = itemReader.GetAttribute("value");
                                Items[name] = value;
                                if (MultiItems.ContainsKey(name))
                                {
                                    MultiItems[name].Add(value);
                                }
                                else
                                {
                                    MultiItems[name] = new List<string>();
                                    MultiItems[name].Add(value);
                                }
 
                            } while (itemReader.Read());
                        }
                    }
                } while (reader.Read());
            }

        }

        public static String UrlExtension
        {
            get
            {
                if (Items.ContainsKey("UrlExtension"))
                {
                    return Items["UrlExtension"];
                }
                else
                {
                    return ".mhtml";
                }
            }
        }
        public static int LocalFileCacheTime
        {
            get
            {
                if (Items.ContainsKey("LocalFileCacheTime"))
                {
                    return int.Parse(Items["LocalFileCacheTime"]);
                }
                else
                {
                    return 60;
                }
            }
        }
        
        public static String ConnectionString
        {
            get
            {
                if (Items.ContainsKey("ConnectionString"))
                {
                    return Items["ConnectionString"];
                }
                else
                {
                    return ConfigurationManager.AppSettings["ConnectStr"];
                }
            }
        }

        public static int ItemsPerPage
        {
            get
            {
                if(Items.ContainsKey("ItemsPerPage"))
                {
                    return int.Parse(Items["ItemsPerPage"]);
                }
                return 20;
            }
        }

        public static int PagePerPage
        {
            get
            {
                if (Items.ContainsKey("PagePerPage"))
                {
                    return int.Parse(Items["PagePerPage"]);
                }
                return 10;
            }
        }

        public static int CacheDuration
        {
            get
            {
                if (Items.ContainsKey("CacheDuration"))
                {
                    return int.Parse(Items["CacheDuration"]);
                }
                return 0;
            }
        }

        public static String LogFile
        {
            get
            {
                if (Items.ContainsKey("LogFile"))
                {
                    return Items["LogFile"];
                }
                return "D:\\LogFile.log";
            }
        }

        private static Encoding encodingValue;
        public static Encoding EncodingValue
        {
            get
            {
                if (encodingValue == null)
                {
                    if (Items.ContainsKey("Encoding"))
                    {
                        String encoding = Items["Encoding"].ToLower();
                        switch (encoding)
                        {
                            case "utf-8":
                                encodingValue = Encoding.UTF8;
                                break;
                            case "unicode":
                                encodingValue = Encoding.Unicode;
                                break;
                            case "ascii":
                                encodingValue = Encoding.ASCII;
                                break;
                            default:
                                encodingValue = Encoding.Default;
                                break;
                        }
                    }
                    else
                    {
                        encodingValue = Encoding.Default;
                    }
                }
                return encodingValue;
            }
        }

        public static void Save()
        {
            XmlDocument doc = new XmlDocument();
            XmlNode root = doc.CreateElement("items");
            doc.AppendChild(root);
            foreach (KeyValuePair<String, String> item in Items)
            {
                XmlNode itemNode = doc.CreateElement("item");
                XmlAttribute attrName = doc.CreateAttribute("name");
                attrName.Value = item.Key;
                XmlAttribute attrValue = doc.CreateAttribute("value");
                attrValue.Value = item.Value;
                itemNode.Attributes.Append(attrName);
                itemNode.Attributes.Append(attrValue);
                root.AppendChild(itemNode);
            }
            doc.Save(ConfigFile);
        }

        public static String GetItemValue(String itemName)
        {
            if(Items.ContainsKey(itemName))
                return Items[itemName];
            return "";
        }

        private static String encrptStr = "";
        private static bool encrptConnStr = false;
        public static bool EncrptConnStr
        {
            get
            {
                if (!String.IsNullOrEmpty(encrptStr))
                {
                    return encrptConnStr;
                }
                if (!Items.TryGetValue("EncrptConnStr", out encrptStr))
                {
                    encrptStr = "false";
                }
                bool.TryParse(encrptStr, out encrptConnStr);
                return encrptConnStr;
            }
        }
    }
}
